home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Dr. Windows 3
/
dr win3.zip
/
dr win3
/
WINPROGS
/
UPC12BS1.ZIP
/
UUCICO
/
SCRIPT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-10-03
|
17KB
|
494 lines
/*--------------------------------------------------------------------*/
/* s c r i p t . c */
/* */
/* Script processing routines for UUPC/extended */
/* */
/* John H. DuBois III 3/31/90 */
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* System include files */
/*--------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
/*--------------------------------------------------------------------*/
/* UUPC/extended include files */
/*--------------------------------------------------------------------*/
#include "lib.h"
#include "dcp.h"
#include "dcpsys.h"
#include "hostable.h"
#include "hlib.h"
#include "modem.h"
#include "script.h"
#include "security.h"
#include "ssleep.h"
#include "catcher.h"
#include "usrcatch.h"
#include "commlib.h"
/*--------------------------------------------------------------------*/
/* Local defines */
/*--------------------------------------------------------------------*/
#define MAXMATCH 64 /* max length of search string; must
be a power of 2 */
#define QINDMASK (MAXMATCH - 1) /* bit mask to get queue index */
#define EOTMSG "\004\r\004\r"
/*--------------------------------------------------------------------*/
/* Internal function prototypes */
/*--------------------------------------------------------------------*/
static int StrMatch(char *MatchStr, char C, char **failure);
/* Internal match routine */
static boolean Match( char *Search,
char *Buffer,
size_t *SearchPos);
static size_t MatchInit( const char *MatchStr );
static void writestr(register char *s);
static void flushScriptBuffer( void );
static void slowWrite( char *s, size_t len);
/*--------------------------------------------------------------------*/
/* Global variables */
/*--------------------------------------------------------------------*/
currentfile();
static char scriptBuffer[40]; // Can be shorter then longest send
// string, as longer strings are
// send without buffering
static size_t scriptBufferLen = 0;
/*--------------------------------------------------------------------*/
/* e x p e c t s t r */
/* */
/* wait for a pattern on input */
/* */
/* expectstr reads characters from input using sread, and */
/* compares them to a search string. It reads characters */
/* until either the search string has been seen on the input */
/* or a specified timeout interval has passed without any */
/* characters being read. */
/* */
/* Global variables: none. */
/* */
/* Input parameters: */
/* Search is the string that is searched for on the input. */
/* Timeout is the timeout interval passed to sread. */
/* */
/* Output parameters: none. */
/* */
/* Return value: */
/* TRUE is returned if the search string is found on input. */
/* FALSE is returned if sread times out. */
/*--------------------------------------------------------------------*/
int expectstr(char *Search, unsigned int Timeout, char **failure)
{
char buf[BUFSIZ];
int result;
time_t quit = time( NULL ) + Timeout;
register char *ptr = buf;
printmsg(2, "wanted \"%s\"", Search);
if (!strlen(Search)) /* expects nothing */
return TRUE;
StrMatch(Search,'\0', failure); /* set up search string */
do {
if (ptr == &buf[BUFSIZ-1])
ptr = buf; /* Save last character for term \0 */
if (sread(ptr , 1, (int) (quit - time(NULL))) < 1)
{
/* The scan failed? */
char *s;
if ( terminate_processing || raised )
return 0;
while ( ptr > buf )
if (*(--ptr) > ' ')
break; /* Locate the last printable char */
*(ptr+1) = '\0'; /* Terminate the string */
for ( s = buf; (*s > '\0') && (*s <= ' '); s++ );
/* Locate the first printable char */
while ( ptr-- > s )/* Zap control chars */
if (*ptr < ' ')
*ptr = '?';
if ( debuglevel < 2 )
printmsg(1, "wanted \"%s\"", Search);
printmsg(1, "got ??? \"%s\"",s );
return FALSE;
} /* if (sread(ptr , 1, Timeout) < 1) */
*ptr &= 0x7f;
result = StrMatch(Search, *ptr++, failure);
} while (!result);
return result;
} /*expectstr*/
/*
* StrMatch: Incrementally search for a string.
* John H. DuBois III 3/31/90
* StrMatch searches for a string in a sequence of characters.
* The string to search for is passed in an initial setup call
* (input character in this call is \0)
* Further calls with the search string pass one
* character per call.
* The characters are built up into an input string.
* After each character is added to the input string,
* the search string is compared to the last length(search string)
* characters of the input string to determine whether the search
* string has been found.
*
* Global variables: none.
*
* Input parameters:
* MatchStr is the string to search for.
* C is the character to add to the input string.
* It is ignored on a setup call.
*
* Output parameters: None.
*
* Return value:
* On the setup call, -1 is returned if the search string is
* longer than the input string buffer. Otherwise, 0 is returned.
*
* On comparison calls,
* 1 is returned if the search string has been found.
* > 1 is returned if a failure string has been found.
* Otherwise 0 is returned.
*/
static int StrMatch(char *MatchStr, char C, char **failure)
{
/*
* The input string is stored in a circular buffer of MAXMATCH
* characters. If the search string is found in the input,
* then the last character added to the buffer will be the last
* character of the search string. Therefore, the string
* compare will always start SearchLen characters behind the
* position where characters are added to the buffer.
*/
static char Buffer[MAXMATCH]; /* Input string buffer */
static size_t PutPos; /* Where to add chars to buffer */
static size_t SearchPos[MAXLIST];
static size_t SearchPosition;